//
//  BC_KeFuAlextView.m
//  BenChiProject
//
//  Created by xue on 2018/4/9.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "BC_KeFuAlextView.h"
///alertView  宽
#define AlertWise SCREEN_WIDTH-40
///各个栏目之间的距离
#define XLSpace 5.0
@interface BC_KeFuAlextView()
// 背景视图
@property (nonatomic, strong) UIView *backgroundView;
//弹窗
@property (nonatomic,retain) UIView *alertView;
//内容
@property (nonatomic,retain) UILabel *msgLbl;
//确认按钮
@property (nonatomic,retain) UIButton *sureBtn;
//取消按钮
@property (nonatomic,retain) UIButton *cancleBtn;
@property (nonatomic,retain) UIButton *phoneBtn;

@end
@implementation BC_KeFuAlextView

- (instancetype)initWithMessage:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle phone:(NSString*)phone delete:(BOOL)delete
{
    
    
    if (self == [super init]) {
        
        self.frame = [UIScreen mainScreen].bounds;
        
        [self addSubview:self.backgroundView];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 4.0;
        self.alertView.frame = CGRectMake(0, 0, AlertWise, 190);
        self.alertView.layer.position = self.center;
        
        self.msgLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 16, AlertWise-2*20, 30)];
        self.msgLbl.text =message;
        self.msgLbl.font =[UIFont boldSystemFontOfSize:18.0];
        self.msgLbl.textColor =[UIColor blackColor];
        self.msgLbl.textAlignment = NSTextAlignmentLeft;
        self.msgLbl.numberOfLines=0;
        [self.alertView addSubview:self.msgLbl];
        
        
        
            
        self.phoneBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _phoneBtn.frame =CGRectMake(20, CGRectGetMaxY(_msgLbl.frame)+4, AlertWise-2*20, 40);
        [_phoneBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_phoneBtn setTitle:phone forState:UIControlStateNormal];
        _phoneBtn.titleLabel.font =[UIFont systemFontOfSize:16.0];
        _phoneBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.alertView addSubview:self.phoneBtn];
        
        
        
          
        //两个按钮
        if (cancleTitle && sureTitle) {
            self.cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            if (delete) {//删除提示
                self.cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(self.phoneBtn.frame)+14, (SCREEN_WIDTH-40)/2, 36);
                _cancleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

            }else{//客服电话
                self.cancleBtn.frame = CGRectMake(AlertWise-80*2-20*2, CGRectGetMaxY(self.phoneBtn.frame)+14, 80, 36);
                _cancleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

            }
            [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
            [self.cancleBtn setTitleColor:UIColorFromRGB(0x2CA69A) forState:UIControlStateNormal];
            self.cancleBtn.tag = 1;
            [self.cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
//            self.cancleBtn.layer.borderColor =RGBCOLOR(138, 138, 137).CGColor;
//            self.cancleBtn.layer.borderWidth =1;
            self.cancleBtn.titleLabel.font =[UIFont systemFontOfSize:16.0];
            [self.alertView addSubview:self.cancleBtn];
            
            
            
            
            
            self.sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            if (delete) {
                self.sureBtn.frame = CGRectMake((SCREEN_WIDTH-40)/2, CGRectGetMaxY(self.phoneBtn.frame)+14,(SCREEN_WIDTH-40)/2 , 36);
                _sureBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            }else{
                self.sureBtn.frame = CGRectMake(AlertWise-80-20, CGRectGetMaxY(self.phoneBtn.frame)+14, 80, 36);
                _sureBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            }
            
            [self.sureBtn setTitle:sureTitle forState:UIControlStateNormal];
            [self.sureBtn setTitleColor:UIColorFromRGB(0x2CA69A) forState:UIControlStateNormal];
            self.sureBtn.tag = 2;
//            self.sureBtn.layer.borderColor =Color_ZhuSeDiao.CGColor;
//            self.sureBtn.layer.borderWidth =1;
            self.sureBtn.titleLabel.font =[UIFont systemFontOfSize:16.0];
            [self.sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
            [self.alertView addSubview:self.sureBtn];
        }
    
        //计算高度
        CGFloat alertHeight = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
        self.alertView.frame = CGRectMake(0, 0, AlertWise, alertHeight+14);
        self.alertView.layer.position = self.center;
        
        [self addSubview:self.alertView];
    }
    return self;
    
    
}
#pragma mark - 弹出 -
- (void)showXLAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}
#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.50];
        _backgroundView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}
#pragma mark - 点击背景遮罩图层事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
- (void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
    }];
}


#pragma mark - 回调 -设置只有2  -- > 确定才回调
- (void)buttonEvent:(UIButton *)sender
{
    if (sender.tag == 2) {
        if (self.resultIndex) {
            self.resultIndex(sender.tag);
        }
    }
    [self removeFromSuperview];
}
@end
